<?php
    $usersall = $user->selectAll();
    if(isset($_POST['submit']))
    {
        $email = @$_POST['loemail'];
        $password = @$_POST['lopass'];
        $users = $user->select($email);
        if($users == null)
        {
             echo("<script>location.href = '".HOST."view/view_errorlogin.php?error=1';</script>");
        }
        if($email == $users[0]['email'] && $password == $users[0]['pass'])
        {
            @$_SESSION['email'] = $users[0]['email'];
            @$_SESSION['name'] = $users[0]['name'];
            @$_SESSION['per'] = $users[0]['per'];
            echo("<script>location.href = '".HOST."index.php';</script>");
        }
        else {
             echo("<script>location.href = '".HOST."view/view_errorlogin.php?error=2';</script>");
        }
    }
    if(isset($_POST['submit2']))
    {
        $name = @$_POST['siname'];
        $email = @$_POST['siemail'];
        $password = @$_POST['sipass'];
        $repassword = @$_POST['resipass'];
        if(strlen($name) > 1 && strlen($name) < 15 && strlen($email) > 10 && strlen($email) < 25 && strlen($password) > 4 && strlen($password) < 25 && strlen($repassword) > 4 && strlen($repassword) < 25)
        {
            if($password == $repassword)
            {
                foreach ($usersall as $useralls) :
                    if($email == $useralls['email'])
                    {
                        echo("<script>location.href = '".HOST."view/view_errorlogin.php?error=5';</script>");
                        $flag = 1;
                    }
                endforeach;
                if($flag == 1)
                {
                    echo("<script>location.href = '".HOST."view/view_errorlogin.php?error=5';</script>");
                }
                else
                {
                    $users = $user->insert($name,$email,$password);
                    $users = $user->select($email);
                    @$_SESSION['email'] = $users[0]['email'];
                    @$_SESSION['name'] = $users[0]['name'];
                    @$_SESSION['per'] = $users[0]['per'];
                    @$_SESSION['block'] = $users[0]['block'];
                    echo("<script>location.href = '".HOST."index.php';</script>");
                }
            }
            else{
                echo("<script>location.href = '".HOST."view/view_errorlogin.php?error=4';</script>");
            }
        }
        else
        {
            echo("<script>location.href = '".HOST."view/view_errorlogin.php?error=3';</script>");
        }
        
    }
?>